var uploadDocPageLayout;
var userDialog;
/*function moveToViewGroup(){
    $("#userList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html();
            $(this).remove();
            $("#viewGroupList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
            $("#viewGroupDiv").append("<input type='hidden' name='viewGroup' value='"+ liId +"'>");
        });
    //$("#viewGroupList").selectable("refresh");
   
}*/
function openSearchUser()
{
	
	$("#userSearchDialogue").dialog("open");
}
function openSearchLinkDocument()
{
	$.post('docLookup.htm',{data:'data'},function(data){
		$("#docSearchDialogue").empty().html(data);
		//uploadDocPageLayout.resizeAll();
	},"html");
	$("#docSearchDialogue").dialog("open");
}
function searchDoc()
{
	var docSearchId=$("#docSearchId").val();
	var docSearchName=$("#docSearchName").val();
	var docSearchLoc=$("#docSearchLoc").val();
	var docSearchDept=$("#docSearchDept").val();
	var docSearchType=$("#docSearchType").val();
	$.getJSON("fetchDoc.htm",{docSearchId:docSearchId,docSearchName:docSearchName,docSearchLoc:docSearchLoc,docSearchDept:docSearchDept,docSearchType:docSearchType},function(data){
	var options ='';
    for (var i = 0; i < data.length; i++) {
      //options += '<tr><td align="center"><a  target="_blank" href="./accessDocDtls/'+data[i].docId +'/'+data[i].docName +'.htm">'+ data[i].docId + '</a></td><td align="left">' + data[i].docName + '</td></tr>';
  	  options += '<tr><td align="center"><a  href="javascript:chooseDoc('+"'" +data[i].docId+"','"+data[i].docName+"'"+');">'+ data[i].docId + '</a></td><td align="left">' + data[i].docName + '</td></tr>';
    }
    $("#docSearchDialogueResult").html(options);
    
  },"html");
}
function chooseDoc(docId,docName)
{
	var options = '<li id="'+docId+'" class="ui-state-default">'+docId + " (" + docName+")"+'</li>';
	 $("#uploadTab").append("<input type='hidden' name='linkedDocs' value='"+ docId +"'>");
	 $("#linkeddocList").append(options);
}
function searchuser()
{
	var userId=$("#userSearchId").val();
	var userName=$("#userSearchName").val();
	$.getJSON("fetchUser.htm",{userid:userId,username:userName},function(data){
	var options ='';
    for (var i = 0; i < data.length; i++) {
      //options += '<tr><td align="center"><a  target="_blank" href="./accessDocDtls/'+data[i].docId +'/'+data[i].docName +'.htm">'+ data[i].docId + '</a></td><td align="left">' + data[i].docName + '</td></tr>';
  	  options += '<tr><td align="center"><a  href="javascript:chooseUser('+"'" +data[i].optionValue+"','"+data[i].optionDisplay+"'"+');">'+ data[i].optionValue + '</a></td><td align="left">' + data[i].optionDisplay + '</td></tr>';
    }
    $("#userSearchDialogueResult").html(options);
    
  },"html");
}
function chooseUser(userid,userName)
{
	var options = '<li id="'+userid+'" class="ui-state-default">'+userid + " (" + userName+")"+'</li>';
	 $("#userList").append(options);
}
function populateUserList()
{
	var locId = ($("#ownerLocId").val());
	$.getJSON("populateUsers.htm",{id: $(this).val(), ajax: 'true',locId: locId}, function(j){
		 var options = '';
		
     for (var i = 0; i < j.length; i++) {
       options += '<li id="'+j[i].optionValue+'" class="ui-state-default">'+j[i].optionValue+" "+'('+j[i].optionDisplay+')</li>';
     }
     $("#userList").html(options);
   });	
}
/*function moveToPrintGroup(){
    $("#userList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html();
            $(this).remove();
            $("#printGroupList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
            $("#printGroupDiv").append("<input type='hidden' name='printGroup' value='"+ liId +"'>");
        });
    //$("#viewGroupList").selectable("refresh");
   
}*/
function showResponse()
{
	
}
function loadAttributes(docType)
{
	$("#attrpart").loading({
		onAjax:true, 
		delay: 50,
		text: "Loading Attributes . . .",
		align:'center',
		pulse: "type"
		});
	$.post('loadAttributes.htm',{documentType:docType},function(data){
		$("#mdAttr").empty().html(data);
		//uploadDocPageLayout.resizeAll();
	},"html");
}
function createLayoutForMetaData(){
	
	
	/*$("#uploadDocTabsPane").tabs({
		event: 'click',
		select : function(event, ui) {
			
			switch (ui.index) {
			case 0:
				
					
				
				break;
			case 1:
				
					
				
				break;
			case 2:
				
					
				
				break;
			case 3:
				
					
				
				break;
			}
		}
	});*/
	
	
	
	
	

}
function submitMetaDataDtls(){
	
	
	var formData = $("#uploadForm").formSerialize();
	
	$.post("uploadDoc.htm",formData,function(data){
		
	},"html");
}

function bindEventsAndFormatUploadDocPage(){
	$("#docTypeID").change(function(){
		var docType = $("#docTypeID").val();
		loadDocCategory();
		loadAttributes(docType);
		});
	$("#docCatID").change(function(){
		loadDocSubCategory();
	});

	/*$('#uploadForm').bind('submit', function() {
		options={iframe: true,success: showDocUploadResponse,async:false,target : "#contentDiv" };
		if(validate()){			
			$(this).ajaxSubmit(options);
		}
		return false; // <-- important!
	//createChangePwdDialog();
	});*/
	
	$("#userList").selectable();
    $("#viewGroupList").selectable();
    $("#printGroupList").selectable();
      
    userDialog = $("#userSearchDialogue").dialog({
   		height: 350,
  		width: 750,
  		modal: true,
  		zIndex: 8999,
  		autoOpen: false
   	});
    
    userDialog = $("#docSearchDialogue").dialog({
   		height: 350,
  		width: 750,
  		modal: true,
  		zIndex: 8999,
  		autoOpen: false
   	});
    populateUserList();
     $("#validityStartDate").datepicker({dateFormat:'dd-mm-yy'});
    $("#validityEndDate").datepicker({dateFormat:'dd-mm-yy'});
    enableDisableFields('docRenewableSelect');
}

function loadDocCategory(){
	var docType = $("#docTypeID").val();
	
	$.getJSON("fetchCategory.htm",{id: $(this).val(), ajax: 'true',parentlevel: docType}, function(j){
		 var options = '<option value='+0+'>'+'ALL'+'</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $("#docCatID").html(options);
    })	;
}

function loadDocSubCategory(){
	var docCat = $("#docCatID").val();
	$.getJSON("fetchSubCategory.htm",{id: $(this).val(), ajax: 'true',parentlevel: docCat}, function(j){
		 var options = '<option value='+0+'>'+'ALL'+'</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $("#docSubCatID").html(options);
    });	
	
}

function uploadDataToServer(formId,containerId){
	var options ={
			target: "#"+containerId,
			type: 'post',
			data: {},
			success: showResponse,
			iframe: true
	};
	$("#"+formId).ajaxSubmit(options); 
}

function showResponse(responseText, statusText, xhr, $form)  { 
	
	var isSuccess = $("#isSuccess").val();
	var hiddenDocCode = $("#docCode").val();
	
	
	createLayoutForMetaData();
	bindEventsAndFormatUploadDocPage();
	if(isSuccess=="true")
	{alert("Document Uploaded with Doc Code"+hiddenDocCode);}
	
	loadDocCategory();
	loadDocSubCategory();
	uploadDocPageLayout.resizeAll();
	
	return true;
}

function loadRenewDtlsMethods()
{
	
	$('#docDtlsRenew').bind('submit', function() {
		options={iframe: true,success: showRenewResponse,async:false,target: "#docDtlsDialogue"};
				
			$(this).ajaxSubmit(options);
		
		return false; // <-- important!

	});
	
	var opsValue = $("#ops").val();
	
	switch (opsValue)
	{
	
	case "1": $("#docAttach").attr("disabled","disabled");
				$("#submitbutton").attr("disabled","disabled");
				$("#linkbutton").attr("disabled","disabled");break;
	
	case "2": $(".renewInputs").attr("readonly",false);
			$("#linkbutton").attr("disabled","disabled");
			$(".renewInputs").attr("disabled",false);
			$("#submitbutton").attr("value","Renew Document");
			$("viewDocAttach").attr("href","#");
			break;
	
	case "3": $(".versionInputs").attr("readonly",false);
	$("#linkbutton").attr("disabled","disabled");
			$("#submitbutton").attr("value","Upload New Version");
			break;
	case "4": $(".linkInputs").attr("readonly",false);
			$("#submitbutton").attr("value","Update Links");
			$("#docAttach").attr("disabled","disabled");break;
	case "5": $("#linkbutton").attr("disabled","disabled");
			$("#submitbutton").attr("value","Activ/Deactivate");
			$("#docAttach").attr("disabled","disabled");
			break;
	
	
	}
	
	docSearchDialog = $("#docSearchDialogue").dialog({
   		height: 350,
  		width: 750,
  		modal: true,
  		zIndex: 8999,
  		autoOpen: false
   	});
}
function showRenewResponse(responseText, statusText, xhr, $form)
{
	if(responseText=="true")
	{
		$("#docDtlsDialogue").dialog("close");
		alert("Document Updated Successfully");
	}
	
		
}

function enableDisableFields(selectId){
	var value = $("#" + selectId).val();
	
	if(value == "false"){
		$("input.disableInputField").each(function(index){
			$(this).attr("disabled","disabled");
			//$(this).addClass('infoTextBox highLightText1');
		});
	}else{
		$("input.disableInputField").each(function(index){
			$(this).removeAttr("disabled");
		});
	}
}